using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ToonyColors
{
	public class TCF_Demo : MonoBehaviour
	{
		private const float LightIntensity_TCF = 3f;

		private const float LightIntensity_Diff = 4f;

		public GUISkin guiSkin;

		public Shader unityDiffuse;

		public Light DirLight;

		public Light PointLight;

		public Material[] Materials;

		private Dictionary<Material, Shader> OriginalShaders = new Dictionary<Material, Shader>();

		private bool viewUnityShader;

		private bool viewPointLight;

		private bool cameraRotate;

		private float cameraSpeed;

		private float targetCamSpeed = 1f;

		private float cameraDir = 1f;

		private bool cameraDirInv;

		private void Awake()
		{
			Material[] materials = Materials;
			foreach (Material material in materials)
			{
				OriginalShaders.Add(material, material.shader);
			}
		}

		private void OnDestroy()
		{
			foreach (KeyValuePair<Material, Shader> originalShader in OriginalShaders)
			{
				originalShader.Key.shader = originalShader.Value;
			}
		}

		private void Update()
		{
			Camera.main.transform.RotateAround(Vector3.zero, Vector3.up, 20f * Time.deltaTime * cameraDir * cameraSpeed);
			if (cameraRotate)
			{
				cameraSpeed = Mathf.Lerp(cameraSpeed, targetCamSpeed, 4f * Time.deltaTime);
			}
			else if (cameraSpeed > 0.01f)
			{
				cameraSpeed = Mathf.Lerp(cameraSpeed, 0f, 4f * Time.deltaTime);
			}
			else
			{
				cameraSpeed = 0f;
			}
			Rect rect = new Rect(16f, 16f, 500f, 140f);
			Vector2 point = Input.mousePosition;
			point.y = (float)Screen.height - point.y;
			if (!rect.Contains(point) && Input.GetMouseButton(0))
			{
				targetCamSpeed = 5f;
			}
			else
			{
				targetCamSpeed = 1f;
			}
		}

		private void OnGUI()
		{
			GUI.skin = guiSkin;
			Rect rect = new Rect(16f, 16f, 500f, 140f);
			GUI.Box(rect, string.Empty);
			rect.xMin += 10f;
			rect.xMax -= 10f;
			rect.yMin += 4f;
			rect.yMax -= 6f;
			GUILayout.BeginArea(rect);
			GUILayout.Label("Demo Settings:", "Header");
			GUILayout.BeginHorizontal();
			GUILayout.Label("Shader: ");
			GUI.enabled = viewUnityShader;
			if (GUILayout.Button("Toony Colors Free", GUILayout.Width(180f)))
			{
				ToonyColorsShader();
				SetLightsIntensity(3f);
				viewUnityShader = false;
			}
			GUI.enabled = !viewUnityShader;
			if (GUILayout.Button("Unity Diffuse", GUILayout.Width(180f)))
			{
				SetShader(unityDiffuse);
				SetLightsIntensity(4f);
				viewUnityShader = true;
			}
			GUILayout.EndHorizontal();
			GUILayout.BeginHorizontal();
			GUILayout.Label("Light: ");
			GUI.enabled = !DirLight.gameObject.activeSelf;
			if (GUILayout.Button("Directional Light", GUILayout.Width(180f)))
			{
				DirLight.gameObject.SetActive(true);
				PointLight.gameObject.SetActive(false);
			}
			GUI.enabled = DirLight.gameObject.activeSelf;
			if (GUILayout.Button("Point Light", GUILayout.Width(180f)))
			{
				PointLight.gameObject.SetActive(true);
				DirLight.gameObject.SetActive(false);
			}
			GUILayout.EndHorizontal();
			GUILayout.BeginHorizontal();
			GUI.enabled = true;
			GUILayout.Label("Camera: ");
			cameraRotate = GUILayout.Toggle(cameraRotate, "Rotation", GUILayout.Width(180f));
			GUI.enabled = cameraRotate;
			if (GUILayout.Button("Inverse Direction", GUILayout.Width(180f)))
			{
				StopAllCoroutines();
				StartCoroutine("CR_InverseDir");
				cameraDirInv = !cameraDirInv;
			}
			GUI.enabled = true;
			GUILayout.EndHorizontal();
			GUILayout.Label("Hold mouse button to speed up rotation!");
			GUILayout.EndArea();
		}

		private IEnumerator CR_InverseDir()
		{
			float initial = cameraDir;
			float target = ((!cameraDirInv) ? (-1f) : 1f);
			float t = 0f;
			float d = 0.75f;
			while (t < d)
			{
				t += Time.deltaTime;
				float delta = Mathf.Clamp01(t / d);
				cameraDir = Mathf.Lerp(initial, target, delta);
				yield return null;
			}
		}

		private void ToonyColorsShader()
		{
			foreach (KeyValuePair<Material, Shader> originalShader in OriginalShaders)
			{
				originalShader.Key.shader = originalShader.Value;
			}
		}

		private void SetShader(Shader shader)
		{
			Material[] materials = Materials;
			foreach (Material material in materials)
			{
				material.shader = shader;
			}
		}

		private void SetLightsIntensity(float intensity)
		{
			PointLight.intensity = intensity;
			DirLight.intensity = intensity;
		}
	}
}
